package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsRecoveryTypeBiz;
import com.project.entity.CmsRecoveryType;
import com.project.mapper.CmsRecoveryTypeMapper;
import com.project.pojo.model.AddRecoveryTypeModel;
import com.project.pojo.model.UpdateRecoveryTypeModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 回收品类业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/13 20:57
 * @Version: 1.0
 */
@Service
public class CmsRecoveryTypeBizImpl extends ServiceImpl<CmsRecoveryTypeMapper, CmsRecoveryType> implements CmsRecoveryTypeBiz {

    /**
     * 获取所有回收品类
     * @return
     */
    public List<CmsRecoveryType> getAllRecoveryType(){
        QueryWrapper<CmsRecoveryType>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("sort");

        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 添加回收品类
     * @param addProductModel
     * @param userId
     * @return
     */
    public int addRecoveryType(AddRecoveryTypeModel addProductModel, Long userId){
        CmsRecoveryType recoveryType = new CmsRecoveryType();
        BeanUtils.copyProperties(addProductModel, recoveryType);
        recoveryType.setCreateTime(new Date());
        recoveryType.setCreateUser(userId);
        recoveryType.setStatus(1);

        return this.baseMapper.insert(recoveryType);
    }

    /**
     * 修改回收品类
     * @param updateRecoveryTypeModel
     * @param userId
     * @return
     */
    public int updateRecoveryType(UpdateRecoveryTypeModel updateRecoveryTypeModel, Long userId){
        CmsRecoveryType recoveryType = this.baseMapper.selectById(updateRecoveryTypeModel.getRecoveryTypeId());
        BeanUtils.copyProperties(updateRecoveryTypeModel, recoveryType);
        recoveryType.setUpdateTime(new Date());
        recoveryType.setUpdateUser(userId);
        return this.baseMapper.updateById(recoveryType);
    }

    /**
     * 删除回收品类
     * @param recoveryTypeId
     * @param userId
     * @return
     */
    public int delRecoveryType(Long recoveryTypeId, Long userId){
        int result = 0;
        CmsRecoveryType recoveryType = this.baseMapper.selectById(recoveryTypeId);
        if (null != recoveryType){
            recoveryType.setUpdateTime(new Date());
            recoveryType.setUpdateUser(userId);
            recoveryType.setStatus(-1);
            result = this.baseMapper.updateById(recoveryType);
        }

        return result;
    }

}
